<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="addRole"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="rows">
          <el-table-column
            align="center"
            type="index"
            :index="(params.page -1) * params.pagesize +1"

            label="序号"
            width="120"
          />
          <el-table-column
            align="center"
            prop="name"
            label="名称"
            width="240"
          />
          <el-table-column align="center" prop="description" label="描述" />
          <el-table-column align="center" label="操作" width="240">
            <template #default="{row}">
              <el-button size="small" @click="assignPermission(row.id)">分配权限</el-button>
              <el-button size="small" @click="editRole(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination
            layout="prev,pager,next"
            :page-size="params.pagesize"
            :total="total"
            :current-page="params.page"
            @current-change="pageChange"
          />
        </el-row>
      </el-card>
    </div>
    <add-role-vue
      ref="addRoles"
      :dialog-visible="dialogVisible"
      :the-refresh="getdepart"
      @close-dialog="dialogVisible=false"
      @updata-List="getdepart"
      @open="getPermissionList"
    />
    <!-- 分配权限点弹框 -->
    <!-- 分配权限的弹层 -->
    <el-dialog
      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible="showAssignDialog"
      @close="closeAssigDialog"
      @open="getPermissionList"
    >
      <el-tree
        ref="perTree"
        :data="permissionList"
        :props="{ label: 'name' }"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
      />
      <!-- 权限点数据展示 -->
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeAssigDialog">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { fetchRoleListAPI, fetchDelRole, assignPermissionAPI, detailsAPI } from '@/api/management'
import { fetchPermissionAPI } from '@/api/permission'
import addRoleVue from './components/add-role.vue'
import transTree from '@/utils/transTree'
export default {
  components: {
    addRoleVue
  },
  data() {
    return {
      rows: [],
      total: 0,
      params: {
        page: 1,
        pagesize: 3
      },
      dialogVisible: false,
      showAssignDialog: false,
      permissionList: [], // 权限点数据
      curId: null
    }
  },
  mounted() {
    this.getdepart()
  },
  methods: {
    // 获取数据
    async getdepart() {
      const res = await fetchRoleListAPI(this.params)
      this.rows = res.rows
      this.total = res.total
    },
    // 分页逻辑
    pageChange(page) {
      console.log(page)
      this.params.page = page
      this.getdepart()
    },
    // 删除
    delRole(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await fetchDelRole(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        // 更新列表
        this.getdepart()
      }).catch(() => {

      })
    },
    // 添加事件
    addRole() {
      this.dialogVisible = true
    },
    // 点击编辑数据回显
    editRole(id) {
      // 使用id请求详情回显操作
      // 直接获取子组件的实例对象,调用它内部的方法直接操作
      this.dialogVisible = true
      // 调用子组件的方法不违反单项数据流
      this.$refs.addRoles.getRoleDetall(id)
    },
    // 索引增加2
    // indexMethod(index) {
    //   return (index + 1) * 2
    // }
    // 分配权限点
    assignPermission(id) {
      // 打开弹框
      this.showAssignDialog = true
      // 拿到选中id赋值到上面的cuId上
      this.curId = id
    },
    // 点击x号关闭弹框
    closeAssigDialog() {
      this.showAssignDialog = false
    },
    // 获取权限点数据
    async getPermissionList() {
      const res = await fetchPermissionAPI()
      this.permissionList = transTree(res)
      const detail = await detailsAPI(this.curId)
      // 通过设置tree方法进行id回显 setCheckedKeys()组件中的方法
      this.$refs.perTree.setCheckedKeys(detail.permIds)
    },
    // 确定分配
    async confirm() {
      // 拿到选中的id
      // id:要给哪个角色分配 角色 id this.curId
      // permIds: 由我们选中项的权限id组成的数组 getCheckedKeys()组件中的方法
      const ids = this.$refs.perTree.getCheckedKeys()
      await assignPermissionAPI({
        id: this.curId,
        permIds: ids
      })
      this.showAssignDialog = false
      this.$message.success('分配成功')
    }
  }
}
</script>

<style lang="less" scoped>

</style>
